#include "stdafx.h"
#include "injector.h"
#include "JmpHooker.h"

using namespace std;

namespace GenericFilter
{
    void Injector::inject(const std::string &processName)
    {
        auto allProcess = XProcess::Process::allProcesses();
        for (auto p : allProcess)
        {
            if (p.name().find(processName) != std::string::npos)
            {
                XProcess::Process::setDebug(true);
                XProcess::Process process(p.id(), XProcess::OpenMode::ReadAndWrite);

                process.injectDllAndRun("InjectedDll", "start", nullptr);
            }
        }
    }

    void Injector::inject(uint64_t pid)
    {
        auto allProcess = XProcess::Process::allProcesses();
        for (auto p : allProcess)
        {
            if (p.id() == static_cast<decltype(p.id())>(pid))
            {
                XProcess::Process::setDebug(true);
                XProcess::Process process(p.id(), XProcess::OpenMode::ReadAndWrite);

                process.injectDllAndRun("InjectedDll", "start", nullptr);
            }
        }
    }

    void Injector::undoInject(const std::string &processName)
    {
        auto allProcess = XProcess::Process::allProcesses();
        for (auto p : allProcess)
        {
            if (p.name().find(processName) != std::string::npos)
            {
                XProcess::Process::setDebug(true);
                XProcess::Process process(p.id(), XProcess::OpenMode::ReadAndWrite);

                process.injectDllAndRun("InjectedDll", "stop", nullptr);
            }
        }
    }

    void Injector::undoInject(uint64_t pid)
    {
        auto allProcess = XProcess::Process::allProcesses();
        for (auto p : allProcess)
        {
            if (p.id() == static_cast<decltype(p.id())>(pid))
            {
                XProcess::Process::setDebug(true);
                XProcess::Process process(p.id(), XProcess::OpenMode::ReadAndWrite);

                process.injectDllAndRun("InjectedDll", "stop", nullptr);
            }
        }
    }

}